package com.qfedu.edu.seckill.searchservice.listener;

import com.qfedu.edu.seckill.searchservice.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author xiaobobo
 * @title: MySQLDataChangeListener
 * @projectName cd-java-2404-seckill-parent
 * @description: MySQL中数据改变的监听
 * @date 2025/1/14  15:01
 */
@Component
@Slf4j
public class MySQLDataChangeListener {

    @Autowired
    private SearchService searchService;


    /**
     * 监听MySQL中数据的改变
     *
     * @param data
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {"mysqlQueue"})
    public void receive(byte[] data, Message message, Channel channel) throws IOException {
        String receiveData = new String(data, "UTF-8");
        log.info("接收到的数据是:" + receiveData);
        searchService.handlerReceiveMySQLData(receiveData);
        //这句话的意思就是手动应答(我手动的告诉你我接收到 队列中的数据了 你可以删除了)
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }


}
